﻿using System;
using System.IO;
using RayDen.Library.Components.SystemComponents;
using RayDen.Library.Core.Primitives;
using RayDen.Library.Data.Xml;
using RayDen.Library.Entity.Frames;
using RayDen.Library.Entity.Interface;
using RayDen.RayEngine;
using RayDen.RayEngine.Corrector;
using RayDen.SmallRT;
using RayEngine.Spectral;

namespace RayDen.GLPlayground
{
    public class App
    {
        public static void Main(string[] args)
        {
            TracerFactory.Instance.PopulateDefault();
            FrameDescription frame = null;

            //(RgbSpectrumInfo)
            //F:\Dev\Frames\RDF\Architecture\
            //F:\Dev\Frames\RDF\outdoor\
            string frameName = @"F:\Dev\Frames\RDF\cornell_bdpt.rdF";
            //string frameName = @"C:\Dev\RayDenFrames\cornell.rdF";
            if (args.Length > 0 && File.Exists(args[0]))
            {
                frameName = args[0];
            }

            var bt = new Bootstrapper();
            bt.Initialize(null);
            //"columns_if_frame.xml";
            string oldDir = Directory.GetCurrentDirectory();



            using (var frameFile = new FileStream(frameName, FileMode.Open, FileAccess.Read))
            {
                using (var reader = new StreamReader(frameFile))
                    frame = (FrameDescription)SerializationService.Deserialize(reader.ReadToEnd(), typeof(FrameDescription));
            }
            frame.FrameName = Path.GetFileNameWithoutExtension(frameName);
            Console.WriteLine("Frame [[{0}]] loaded, start setuping renderer", frame.FrameName);

            string ImageDir = Path.GetFullPath(@"..\..\..\Images\");

            //GlobalConfiguration.Instance.SaveConfig("Global.Config.xml");
            //return;
            Directory.SetCurrentDirectory(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));

            GlobalConfiguration.Instance.OpenConfig(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\Global.Config.xml");

            IRenderConfig re =
                //new Renderer();
                //new EngineConfig();
               // new SmallRtConfig();
               new RayEngineConfiguration();
            //new CorrectorEngineConfiguration();
            //new MegaRay.Renderer();

            //(re as RayEngineConfiguration).OpenSceneConfiguration(@"F:\Dev\Frames\Config\cornell.sceneconfig");
            re.OpenFrame(frame);

            using (var example = new TexturedQuadWindow(re))
            //using (var example = new MainWindow(re))
            {
                example.Run(0.067);
            }
        }
    }
}
